package com.roncoo.education.user.service.api.pc.biz;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.roncoo.education.course.feign.interfaces.IFeignTag;
import com.roncoo.education.course.feign.qo.TagQO;
import com.roncoo.education.user.common.dto.UserTagDTO;
import com.roncoo.education.user.common.req.UserTagPageREQ;
import com.roncoo.education.user.common.req.UserTagSaveREQ;
import com.roncoo.education.user.common.req.UserTagUpdateREQ;
import com.roncoo.education.user.common.resq.UserTagPageRESQ;
import com.roncoo.education.user.service.dao.UserTagDao;
import com.roncoo.education.user.service.dao.impl.mapper.entity.UserExample;
import com.roncoo.education.user.service.dao.impl.mapper.entity.UserTag;
import com.roncoo.education.user.service.dao.service.MyUserTagService;
import com.roncoo.education.util.base.Page;
import com.roncoo.education.util.base.PageUtil;
import com.roncoo.education.util.base.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class PcApiUserTagBiz {

    @Autowired
    private UserTagDao dao;

    @Autowired
    private IFeignTag feignTag;

    @Autowired
    private MyUserTagService userTagService;

    /**
     * 标签用戶分页列出
     * @param req
     * @return
     * @author wangrong
     */
    public Result<Page<UserTagPageRESQ>> listForPage(UserTagPageREQ req) {
        UserExample example = new UserExample();
        Page<UserTag> page = dao.listForPage(req.getPageCurrent(), req.getPageSize(), example);
        return Result.success(PageUtil.transform(page, UserTagPageRESQ.class));
    }

    @Transactional
    public Result<String> save(UserTagSaveREQ userTagSaveREQ) {

        if (userTagSaveREQ.getUserIds().isEmpty()) {
            return Result.error("包含用户不能为空");
        }

        Long tagId = feignTag.save(new TagQO().setTagName(userTagSaveREQ.getTagName()));
        userTagSaveREQ.getUserIds().forEach(userId -> userTagService.save(new UserTagDTO().setTagId(tagId).setUserNo(userId)));

        return Result.success("");
    }

    @Transactional
    public Result<String> update(UserTagUpdateREQ updateREQ) {

        if (ObjectUtils.isEmpty(updateREQ.getTagId())) {
            return Result.error("标签id不能为空");
        }
        //修改标签名称
        if (StringUtils.isNotEmpty(updateREQ.getTagName())) {
//            根据标签id修改名称
            TagQO tagQO = new TagQO().setTagName(updateREQ.getTagName()).setId(Long.parseLong(updateREQ.getTagId().toString()));
            feignTag.updateById(tagQO);
        }
        List<UserTagDTO> userTagDTOS = userTagService.list(Wrappers.<UserTagDTO>lambdaQuery().eq(UserTagDTO::getTagId, updateREQ.getTagId()));
        //删除的用户
        List<UserTagDTO> deleteIds = userTagDTOS.stream().filter(tb -> !updateREQ.getUserIds().contains(tb.getUserNo())).collect(Collectors.toList());
        //新增的用户
        List<Long> collect = userTagDTOS.stream().map(UserTagDTO::getUserNo).collect(Collectors.toList());
        List<UserTagDTO> insertIds = userTagDTOS.stream().filter(userTagDTO -> !collect.contains(userTagDTO.getUserNo())).collect(Collectors.toList());

        if (deleteIds.size() > 0) {
            userTagService.removeByIds(deleteIds.stream().map(UserTagDTO::getId).collect(Collectors.toList()));
        }
        if (insertIds.size() > 0) {
            userTagService.saveBatch(insertIds);
        }

        return Result.success("");
    }

}
